/**
 * Created by IntelliJ IDEA.
 * User: 12
 * Date: 12-5-3
 * Time: 上午9:29
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    $('#add').dialog({
        title:'添加',
        width:380,
        height:220,
        modal:true,
        closed:true,
        //draggable:false,
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:okClientHandler
        },{
            text:'取消',
            handler:function(){
                $('#add').dialog('close');
            }
        }]
    });

    $('#addClientUser').dialog({
        title:'添加',
        width:380,
        height:360,
        modal:true,
        closed:true,
        //draggable:false,
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:okHandler
        },{
            text:'取消',
            handler:function(){
                $('#addClientUser').dialog('close');
            }
        }]
    });

    $('#searchDg').dialog({
        title:'查询',
        width:280,
        height:100,
        modal:true,
        closed:true,
        //draggable:false,
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:okSearchHandler
        },{
            text:'取消',
            handler:function(){
                $('#searchDg').dialog('close');
            }
        }]
    });

    function okHandler(){
        $('#add_user_form').form('submit',{
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(data) {
                var data = eval('('+data+')');
                if (data.status == '1'){
                    $('#addClientUser').dialog('close');      // close the dialog
                    $('#client_user_tab').datagrid('reload');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: data.msg
                    });
                }
            }

        });
    };

    function okClientHandler(){
        $('#add_form').form('submit',{
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(data) {
                var data = eval('('+data+')');
                if (data.status == '1'){
                    $('#add').dialog('close');      // close the dialog
                    $('#clientTree').tree('reload');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: data.msg
                    });
                }
            }

        });
    };

    function okSearchHandler(){
        var name = $("#searchDg input[name='searchName']").val();
        name = $.trim(name);
        var selected = $('#clientTree').tree('getSelected');
        $('#searchDg').dialog('close');
        if(!selected) {
            return;
        }
        $('#client_user_tab').datagrid({
            url:'clientUserTreeJson' ,
            queryParams:{clientId:selected.id,name:name}
        })

    }


    function lstAdd() {
        var selected = $('#clientTree').tree('getSelected');
        if(!selected) {
            $.messager.alert('提示','选择用户组','info');
            return;
        }
        $('#addClientUser').dialog('open').dialog('setTitle','添加');
        $('#addClientUser').dialog('open');
        $('#add_user_form').form("clear");
        if (selected){
            $("#clientId").val(selected.id) ;
            $("#clientName").val(selected.text) ;
        }
    }
    function lstUpdate() {
        var selected = $('#client_user_tab').datagrid('getSelected');
        $('#add_user_form').form("clear");
        if (selected){

            $('#add_user_form').form("load", selected);
            $("#add_user_form input[name='vip']").attr("checked",selected.vip);
            var types = selected.type;
            for(var i=0;i<types.length;i++){
                $("#add_user_form input[value="+types[i]+"]").attr("checked",true)
            }
            $('add_user_form #vip').attr("checked",selected.vip);
            $('#addClientUser').dialog('open').dialog('setTitle','编辑');

        }
    }
    function lstDelete() {
        var row = $('#client_user_tab').datagrid('getSelected');
        if (row){
            $.messager.confirm('提示','确定删除该记录?',function(r){
                if (r){
                    $.post($('#userDeleteUrl').val(),{id:row.id},function(result){
                        if (result.status == '1'){
                                $('#client_user_tab').datagrid('reload');
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    },'json');
                }
            });
        }
    }

    function lstSearch(){
        $('#searchDg').dialog("open")
    }

    $(".client-add").live('click',function(){
        var selected = $('#clientTree').tree('getSelected');
        $('#add').dialog('open');
        $('#add_form').form("clear");
        $("#clientParentName").val("根节点") ;
        if (selected){
            $("#clientParentId").val(selected.id) ;
            $("#clientParentName").val(selected.text) ;
        }
    }) ;


    $(".client-update").live('click',function(){
        var selected = $('#clientTree').tree('getSelected');

        if (selected){
            var parentNode =  $('#clientTree').tree('getParent',selected.target);
            $('#add').dialog('open').dialog('setTitle','编辑');
            $('#add_form').form("clear");
            if(parentNode) {
                $("#clientParentId").val(parentNode.id);
                $("#clientParentName").val(parentNode.text);
            }else{
                $("#clientParentName").val('根节点');
            }
            $("#dgClientId").val(selected.id);
            $("#dgClientName").val(selected.text);
        }
    }) ;
    $(".client-delete").live('click',function(){
        var row = $('#clientTree').tree('getSelected');
        if (row){
            $.messager.confirm('提示','确定删除该记录?',function(r){
                if (r){
                    $.post($('#deleteUrl').val(),{id:row.id},function(result){
                        if (result.status == '1'){
                            $('#clientTree').tree('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    },'json');
                }
            });
        }

    } );

    $(".treegrid-add").click(lstAdd)
    $(".treegrid-edit").click(lstUpdate)
    $(".treegrid-remove").click(lstDelete)

    $('#clientTree').tree({
        onClick: function(node){
            if(!node) {
                return;
            }
            $('#client_user_tab').datagrid({
                url:'clientUserTreeJson' ,
                queryParams:{clientId:node.id}
            })
        }
    });

    $('#client_user_tab').datagrid({
        title:'用户列表',
        nowrap: false,
        rownumbers: true,
        singleSelect:true,
        pagination:true,
        idField:'id',
        frozenColumns:[[
            {title:'名称',field:'text',width:200,
                formatter:function(value){
                    return '<span style="color:red">'+value+'</span>';
                }
            }
        ]],
        toolbar:[{
            id:'btnadd',
            text:'添加',
            iconCls:'icon-add',
            handler:lstAdd
        },{
            id:'btnedit',
            text:'编辑',
            disabled:true,
            iconCls:'icon-edit',
            handler:lstUpdate
        },'-',{
            id:'btnremove',
            text:'删除',
            disabled:true,
            iconCls:'icon-remove',
            handler:lstDelete
        },'-',{
            id:'btnsearch',
            text:'查询',
            iconCls:'icon-search',
            handler:lstSearch
        }],
        columns:[[
            {field:'code',title:'编号',width:250},
            {field:'description',title:'备注',width:250},
            {field:'opt',title:'操作',width:100,align:'center',
                formatter:function(value,rec){
                    return '<span class="show_router" style="cursor:pointer;color:red">查看光路</span>';
                }
            }
        ]],
        onBeforeLoad:function(row,param){
           /* if (row){
                $(this).treegrid('options').url = 'treegrid_subdata.json';
            } else {
                $(this).treegrid('options').url = 'treegrid_data.json';
            }*/
        },
        onContextMenu: function(e,row){
            e.preventDefault();
            $(this).treegrid('unselectAll');
            $(this).treegrid('select', row.id);
            $('#mm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        },onClickRow:function(row) {
            $('#btnedit').linkbutton('enable');
            $('#btnremove').linkbutton('enable');

        }
    });

    //任务窗口
    $('#clientUser_route_dg').dialog({
        title:'光路拓扑',
        width:980,
        height:420,
        modal:true,
        closed:true,
        buttons:[{
            text:'关闭',
            handler:function(){
                $('#clientUser_route_dg').dialog('close');
            }
        }]
    });

    $(".show_router").live("click",function(){
        var selected = $('#client_user_tab').datagrid('getSelected');
        if(!selected){
            $.messager.alert('提示','请选择用户','info');
            return;
        }
        var cuId = selected.id;
        var url = $("#clientUserRouterUrl").val();
        url += "?clientUserId="+cuId
        $('#clientUser_route_ifr').attr("src",url)
        $('#clientUser_route_dg').dialog("open")
    })
})

